/*
 * "Hello World" example.
 *
 * This example prints 'Hello from Nios II' to the STDOUT stream. It runs on
 * the Nios II 'standard', 'full_featured', 'fast', and 'low_cost' example
 * designs. It runs with or without the MicroC/OS-II RTOS and requires a STDOUT
 * device in your system's hardware.
 * The memory footprint of this hosted application is ~69 kbytes by default
 * using the standard reference design.
 *
 * For a reduced footprint version of this template, and an explanation of how
 * to reduce the memory footprint for a given application, see the
 * "small_hello_world" template.
 *
 */

#include <stdio.h>
#include <system.h>
#include <io.h>

int read( )
{
    int data;

    while( 1 )
    {
        data = IORD_32DIRECT( PS2_BASE, 0 );
        if( ( ( data >> 16) & 0xFFFF ) != 0 )
        {
            printf( "%02X\n", ( data & 0xFFFFFF ) ); 
            return data;
        }
    }
    
    return data;
}

int main()
{
    printf("Hello from Nios II!\n");

    IOWR_8DIRECT( PS2_BASE, 0, 0xFF );
    read( );
    
    IOWR_8DIRECT( PS2_BASE, 0, 0xFF );
    read( );
    IOWR_8DIRECT( PS2_BASE, 0, 0xFF );
    read( );
    IOWR_8DIRECT( PS2_BASE, 0, 0xF3 );
    read( );
    IOWR_8DIRECT( PS2_BASE, 0, 0xC8 );
    read( );
    IOWR_8DIRECT( PS2_BASE, 0, 0xF3 );
    read( );
    IOWR_8DIRECT( PS2_BASE, 0, 0x64 );
    read( );
    IOWR_8DIRECT( PS2_BASE, 0, 0xF3 );
    read( );
    IOWR_8DIRECT( PS2_BASE, 0, 0x50 );
    read( );
    IOWR_8DIRECT( PS2_BASE, 0, 0xF2 );
    read( );
    IOWR_8DIRECT( PS2_BASE, 0, 0xE8 );
    read( );
    IOWR_8DIRECT( PS2_BASE, 0, 0x03 );
    read( );
    IOWR_8DIRECT( PS2_BASE, 0, 0xE6 );
    read( );
    IOWR_8DIRECT( PS2_BASE, 0, 0xF3 );
    read( );
    IOWR_8DIRECT( PS2_BASE, 0, 0x28 );
    read( );
    IOWR_8DIRECT( PS2_BASE, 0, 0xF4 );
    read( );
    
    printf( "setting end\n" );
    
    while( 1 )
    {
        read( );
    }

    printf( "program end\n" );
    return 0;
}
